/*!
 * 我的语音选择模块
 * @author Sherlock 
 * ---------------------------
 * soundChooseCallback语音选择确认后的回调
 */
require( './style.less' );
define( [
  'jquery',
  'require',
  'bootstrap',
  'message',
  'api',
  'artTemplate'
], function( $ ) {
  var returnParam = {};
  var ShowTitleMessage = require( 'message' );
  var showTime = ShowTitleMessage.showTime;
  var BIGWE_API = require( 'api' );
  var at = require( 'artTemplate' );

  var $me = returnParam.$selector = $( '#my_sound_choose' ),
    Mpnews = {};
  var queryParam = {
    'key': '',
    //'custom_group_id': '',
    'page_num': 1,
    'done': 'voice',
    'l': 12
  }
  var isLoading = 0,
    isNoData = 0;

  var result = [];
  $me.on( 'shown.bs.modal', function() {
    $( this ).show();
    if ( $( '#sound_list li', this ).length ) return;
    if ( !Mpnews.firstLoad ) {
      Mpnews.firstLoad = !0;
      ajaxRequest();
    }
  } ).on( 'click', '.search_btn', function() {
    Mpnews.pageRest = 0;
    $.extend( queryParam, {
      page_num: 1,
      key: $( '.search_input', $me ).val()
    } )
    ajaxRequest();
  } ).on( 'click', '.height_cacu button', function() {
    return;
    // $( this ).addClass( 'selected' ).siblings().removeClass( 'selected' );
    // $.extend( queryParam, {
    // 	p: 1,
    // 	custom_group_id: $( this ).attr( 'group_id' ) || ''
    // } );
    // Mpnews.pageRest = 0;
    // ajaxRequest();

  } ).on( 'click', '#sound_sure', function() {

    var selected = $( '#sound_list li.selected', $me );

    if ( selected.length ) {
      if ( typeof returnParam.soundChooseCallback === 'function' ) {
        returnParam.soundChooseCallback.call( $me, {
          'soundObj': result[ selected.index() ]
        } );
      }
      $( '#sound_list' ).find( 'audio' )[ 0 ].pause();
      $me.modal( 'hide' );
    } else {
      ShowTitleMessage.showWarning( '请选择一个语音', showTime );
    }
  } ).on( 'click', '.more', function() {
    return;
    // if ( $( this ).hasClass( 'showmore' ) ) {
    // 	$( this ).removeClass( 'showmore' );
    // 	$( '.group_list', $me ).height( 28 );
    // } else {
    // 	$( this ).addClass( 'showmore' );
    // 	$( '.group_list', $me ).height( $( '.height_cacu', $me ).height() );
    // }
  } ).on( 'click', '.creategraph', function() {
    $me.modal( 'hide' );
    $( '#sound_upload' ).modal( 'show' );
    // $('#sound_upload').on('shown.bs.modal', function () {
    //   $('body').addClass('modal-open');
    //   $('body').prop('style','padding-right: 17px;');
    // });
    $( '#sound_upload' ).on( 'hidden.bs.modal', function() {
      $( 'body' ).prop( 'style', '' );
    } );


  } );

  $( '#sound_list', $me ).on( 'click', 'li .choice', function() {
    $( this ).parents( 'li' ).addClass( 'selected' ).siblings().removeClass( 'selected' );
  } );
  //语音播放
  $( '#sound_list', $me ).on( 'click', 'li .play', function() {
    if ( $( 'audio' ).prop( 'src' ) != $( this ).attr( 'sound_src' ) ) {
      $( this ).parents( '#sound_list' ).find( 'audio' ).prop( 'src', $( this ).attr( 'sound_src' ) );
    }
    $( this ).parents( 'li' ).addClass( 'selected' ).siblings().removeClass( 'selected' );
    if ( $( this ).parents( '#sound_list' ).find( 'audio' )[ 0 ].paused ) {
      $( this ).parents( '#sound_list' ).find( 'audio' )[ 0 ].play();
      $( this ).find( 'span' ).removeClass( 'icon-bofang' );
      $( this ).find( 'span' ).addClass( 'icon-zanting' );
    } else {
      $( this ).parents( '#sound_list' ).find( 'audio' )[ 0 ].pause();
      $( this ).find( 'span' ).addClass( 'icon-bofang' );
      $( this ).find( 'span' ).removeClass( 'icon-zanting' );
    }

  } );

  function pageRander( total ) {
    $( '.pageControl', $me ).bwPagination( {
      total: total,
      pageCount: queryParam.l,
      autoVisibility: !0,
      callback: function( index ) {
        $.extend( queryParam, {
          page_num: index
        } );
        ajaxRequest();
      }
    } );
  }

  function timeToStr( time ) {
    var h = 0,
      m = 0,
      s = 0,
      _h = '00',
      _m = '00',
      _s = '00';
    time = parseInt( time );
    h = Math.floor( time / 3600 );
    time = Math.floor( time % 3600 );
    m = Math.floor( time / 60 );
    s = Math.floor( time % 60 );
    _s = s < 10 ? '0' + s : s + '';
    _m = m < 10 ? '0' + m : m + '';
    _h = h < 10 ? '0' + h : h + '';
    return _m + ":" + _s;
  }

  function ajaxRequest( fn ) {
    isLoading = 1;
    $( '.spinner', $me ).show();
    $( '#sound_list ul', $me ).empty();
    $.ajax( {
      type: 'post',
      url: BIGWE_API.COMMON.GET_MATERIAL_MPNEWS,
      data: queryParam,
      success: function( data ) {
        data = typeof data === 'string' ? $.parseJSON( data ) : data;
        if ( !data.ret ) {
          detailData = data.data;

          if ( !Mpnews.pageRest ) {
            Mpnews.pageRest = !0;
            pageRander( detailData.total_num );
          }
          //分组
          // if ( !Mpnews.groupLoad ) {
          // 	Mpnews.groupLoad = !0;
          // 	var groupHtml = '';
          // 	for ( i in detailData.customs ) {
          // 		var group = detailData.customs[ i ];
          // 		groupHtml += '<button type="button" group_id="' + group.id + '">' + group.group_name + '</button>';
          // 	}
          // 	var cacuHieight = $( '.height_cacu', $me ).append( groupHtml ).height();
          // 	if ( cacuHieight > 34 ) {
          // 		$( '.more', $me ).show();
          // 	}
          // }

          if ( !detailData.videos.length ) {
            ShowTitleMessage.showWarning( '没有查询到数据', showTime );
            return;
          }
          result = detailData.videos;
          for ( var i in detailData.videos ) {
            for ( var j in detailData.videos[ i ] ) {
              if ( j == 'length' ) {
                var _duration = detailData.videos[ i ][ j ];
                detailData.videos[ i ][ j ] = timeToStr( _duration );
              }
            }

          }
          var appendHtml = at( 'sound_html_item', {
            list: detailData.videos
          } );
          $( '#sound_list ul', $me ).html( appendHtml );
        }

        if ( fn ) {
          fn.call( $me );
        }

      },
      complete: function() {
        isLoading = 0;
        $( '.spinner', $me ).hide();
      }
    } );
  }

  return returnParam;
} );
